﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XWord.Fundamental.Domain;
using XWord.Fundamental.Data.Impl;
using NHibernateUnitOfWork;
using NHibernate.Criterion;

namespace XWord.GUI.Views
{
    /// <summary>
    /// Interaction logic for Window5.xaml
    /// </summary>
    public partial class EtymaLibraryFinderView : Window
    {
        IList<EtymaWordLibrary> EtymaWordLibraryList;
        IList<EtymaLibrary> EtymaLibraryList;
        public EtymaLibraryFinderView()
        {
            InitializeComponent();
            //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback((object obj) => 
            //    {
            //        EtymaWordLibraryList = UnitOfWork.CurrentSession.CreateCriteria<EtymaWordLibrary>().List<EtymaWordLibrary>();
            //        EtymaLibraryList = UnitOfWork.CurrentSession.CreateCriteria<EtymaLibrary>().List<EtymaLibrary>();
            //    }));
           
            textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
            listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);
        }

        void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EtymaLibrary ee = listBox1.SelectedItem as EtymaLibrary;
            if (ee == null) return;
            listBox2.ItemsSource = UnitOfWork.CurrentSession.CreateCriteria<EtymaWordLibrary>()
                .Add(Restrictions.Eq("WordIndex", ee.WordIndex))
                .List<EtymaWordLibrary>();
                
              //  EtymaWordLibraryList.Where(eee => eee.WordIndex == ee.WordIndex).OrderBy(eee => eee.Name).ToList();
           
        }

        void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
           
            listBox1.ItemsSource = UnitOfWork.CurrentSession.CreateCriteria<EtymaLibrary>()
                .Add(Restrictions.Like("Name", textBox1.Text, MatchMode.Start))
                .AddOrder(Order.Asc("Name")).SetMaxResults(20).List<EtymaLibrary>();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox3.Text = string.Empty;
            if (EtymaWordLibraryList.Any(ee => ee.Name.ToLower().Contains(textBox2.Text.ToLower().Trim())))
            {
                //UnitOfWork.CurrentSession.CreateSQLQuery
                //    ("select * from EtymaWordLibraryList as ewl join EtymaWordLibrary on ewl.Name like %");
                var a = UnitOfWork.CurrentSession.CreateCriteria<EtymaLibrary>("el")
                    .CreateCriteria("EtymaWordLibrarys", "ewl")
                     .Add(Restrictions.Like("ewl.Name", textBox2.Text.ToLower().Trim()))
                     .Add(Restrictions.Eq("ewl.WordIndex","el.WordIndex"))
                     .List<EtymaLibrary>();
                     
                   
                //var a = (from ew in EtymaWordLibraryList
                //         from et in EtymaLibraryList
                //        where ew.Name.ToLower().Contains("word")
                //        && ew.WordIndex == et.WordIndex
                //        select et.Name).Distinct();
               
                //List<string> aa = a.ToList();
                //foreach (var item in aa)
                //{
                //    textBox3.Text+= item+"\n";
                //}

                //var results=UnitOfWork.CurrentSession.CreateCriteria<EtymaWordLibraryList>()
                //    .CreateCriteria("","")
                //    .Add(Restrictions.Like(
                
            }
        }
    }
}
